package daoImpls;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import vo.Category;
import vo.PetObject;
import vo.Role;

public class RoleDAOImpl<E> extends ObjectDAOImpl {

    public void add(Role hero) {
        String sql = "insert into role values(null,?)";
        try(Connection c = cp.getConnection(); PreparedStatement s = c.prepareStatement(sql)) {
            s.setString(1,hero.getrName());
            s.execute();
            s.close();
            cp.returnConnection(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void update(Role hero) {
        String sql = "update role set rName=? where rid =  " + hero.getRid();
        try(Connection c = cp.getConnection(); PreparedStatement s = c.prepareStatement(sql)) {
            s.setString(1,hero.getrName());
            s.execute();
            s.close();
            cp.returnConnection(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(String id) {
        super.delete(id,"role");
    }

    @Override
    public Role get(String id) {

        Role role = null;
        try(Connection c = cp.getConnection(); Statement s = c.createStatement()) {
            ResultSet rs = s.executeQuery("select * from role where RID = " +id);          
            if (rs.next()){
                role = new Role();
                role.setRid(rs.getString(1));
                role.setrName(rs.getString(2));
            }
            s.close();
            cp.returnConnection(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return role;
    }


	@Override
	public List<PetObject> list() {
		 List<PetObject> list = new ArrayList<>();
		 Role role;
	        try(Connection c = cp.getConnection();){
	        	PreparedStatement p = c.prepareStatement("SELECT * FROM role");
	        	ResultSet rs = p.executeQuery();
	        	while(rs.next()){
	        		role = new Role();
	        		role.setRid(rs.getString(1));
	        		role.setrName(rs.getString(2));
	        		list.add(role);
	        	}
	        	p.close();
	        	cp.returnConnection(c);
	        } catch (SQLException e) {
				e.printStackTrace();
			}
		return list;
	}
    
    
}
